import 'package:flutter/material.dart';
import 'package:hxdj/common/components/my_alert.dart';
import 'package:hxdj/common/utils/my_device.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:flutter_native_setting_plugin/flutter_native_setting_plugin.dart';


class MyPermission {
  static Future<bool> requestPermission(BuildContext context) async {
    late PermissionStatus status;
    // 1、读取系统权限的弹框
    if (MyDevice.isIOS) {
      status = await Permission.photosAddOnly.request();
    } else {
      status = await Permission.storage.request();
    }
    // 2、假如你点not allow后，下次点击不会在出现系统权限的弹框（系统权限的弹框只会出现一次），
    // 这时候需要你自己写一个弹框，然后去打开app权限的页面
    if (status != PermissionStatus.granted) {
      // ignore: use_build_context_synchronously
      MyAlertManager.showAlert(AlertType.iosMode, context, "请在iPhone的\"设置-隐私-相册\"中允许\"APP\"访问相册", "",
          close: "取消", sure: "去设置", onSure: () {
            // 打开app权限的页面
        FlutterNativeSettingPlugin().openNativeSetting();
      });
      return false;
    } else {
      return true;
    }
  }
}
